package ui.skins 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.globalization.StringTools;

	
	/**
	 * ...
	 * @author SChabanov
	 */
	public class SkinParser 
	{
		
		public function SkinParser() 
		{
			throw new Error("static class")
		}
		
		
		public static function parse(template:Class,name:String):Vector.<DisplayObject>
		{
			var skins:Vector.<DisplayObject> = new Vector.<DisplayObject>();
			var skinData:Object = template == null ? null : new template();
		
			switch (name) 
			{
				case "SButton":
					if (!skinData)
					{
						skinData = new BaseButtonSkin();
					}
					fillSkinChildren(skinData,["skinUp","skinOver","skinDown", "0skinDisabled"],skins);
				break;
				case "SLabelButton":
					if (!skinData)
					{
						skinData = new LabelButtonSkin();
					}
					fillSkinChildren(skinData,["skinUp","skinOver","skinDown", "0skinDisabled","label"],skins);
				break;
				case "SIcon":
					if (!skinData)
					{
						skinData = new IconSkin();
					}
					fillSkinChildren(skinData,["skinUp","0skinOver","0skinDown", "0skinDisabled"],skins);
				break;
				
				default:
					
			}
			return skins;
		}
		
		private static function fillSkinChildren(data:Object,names:Array,out:Vector.<DisplayObject>):void
		{

			//0 - start name is  optional
			for (var i:int = 0; i < names.length; i++) 
			{
				var name:String = names[i];
				
				if (name.charAt(0) == "0") {
					if (data[name.slice(1)] != null)
					{
						name = name.slice(1);
					}else {
						continue;
					}
				}
				if (data[name] == null)
				{
					throw new Error("Skin Parse Error: property name " + name + " not exist");
				}else {
					out.push(data[name]);
				}
			}
		}
		
		
	}

}